package cn.iocoder.yudao.module.hs.controller.admin.qcorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.qcorder.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.qcorder.QcOrderDO;
import cn.iocoder.yudao.module.hs.service.qcorder.QcOrderService;

@Tag(name = "管理后台 - 质检单信息")
@RestController
@RequestMapping("/hs/qc-order")
@Validated
public class QcOrderController {

    @Resource
    private QcOrderService qcOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建质检单信息")
    @PreAuthorize("@ss.hasPermission('hs:qc-order:create')")
    public CommonResult<Long> createQcOrder(@Valid @RequestBody QcOrderSaveReqVO createReqVO) {
        return success(qcOrderService.createQcOrder(createReqVO));
    }

    @PostMapping("/defect-handle")
    @Operation(summary = "异常处理")
    @PreAuthorize("@ss.hasPermission('hs:qc-order:create')")
    public CommonResult<Long> defectHandle(@Valid @RequestBody QcOrderSaveReqVO createReqVO) {
        return success(qcOrderService.defectHandle(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质检单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:qc-order:delete')")
    public CommonResult<Boolean> deleteQcOrder(@RequestParam("id") Long id) {
        qcOrderService.deleteQcOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质检单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:qc-order:query')")
    public CommonResult<QcOrderRespVO> getQcOrder(@RequestParam("id") Long id) {
        QcOrderDO qcOrder = qcOrderService.getQcOrder(id);
        return success(BeanUtils.toBean(qcOrder, QcOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质检单信息分页")
    @PreAuthorize("@ss.hasPermission('hs:qc-order:query')")
    public CommonResult<PageResult<QcOrderRespVO>> getQcOrderPage(@Valid QcOrderPageReqVO pageReqVO) {
        return success(qcOrderService.getQcOrderPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质检单信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:qc-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQcOrderExcel(@Valid QcOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "质检单信息.xls", "数据", QcOrderRespVO.class,
                qcOrderService.getQcOrderPage(pageReqVO).getList());
    }

}